home *** CD-ROM | disk | FTP | other *** search
/ Amiga Packmags / Source, The - Issue 5 (1993)(Epsilon)[WB].zip / Source, The - Issue 5 (1993)(Epsilon)[WB].adf / Text / HowToCode5.lha / howtocode5.txt
Text File  |  1993-03-19  |  76KB  |  2,177 lines

  1.            How to write demos that work (Version 5) - 18/3/93
  2.            ===================================================
  3.  
  4.                (or the Amiga Demo Coders Reference Manual)
  5.  
  6.                 Edited by Comrade J/SAE (ex demo maniac)
  7.  
  8.                  Co-Editor post vacant (apply by email)
  9.  
  10.  
  11. * Please note this is a REPLACEMENT to text files howtocode1.txt
  12. through howtocode4.txt. Sysops, please remove these earlier files
  13. as they contain many mistakes. Thanks in advance...*
  14.  
  15. Thanks to:  Vic Ricker, Grue, Timo Rossi, Jesse Michael, John Derek Muir,
  16. Boerge Noest, Christopher Klaus, Doz/Shining, Andrew Patterson, Walter
  17. Dao, Chris Green, Magnus Timmerby, Patrik Lundquist, Raymond Penners,
  18. the otherwise anonymous u920659@daimi.aau.dk, Matthew Arnold, TGR/Anthrox,
  19. Tero Lehtonen, Carl-Henrik Sk}rstedt (that's how it's spelt via
  20. 7-bit ASCII!!), Arno Hollosi, Irmen de Jong and Jonas Matton
  21. for their comments and contributions.
  22.  
  23. Thanks also to CS who didn't want a credit but I'd like to say
  24. thank you anyway...
  25.  
  26. Introduction
  27. ============
  28.  
  29. This file has grown somewhat from the file uploaded over
  30. Christmas 1992. I've been very busy over the last two months,
  31. so sorry that I haven't been able to update this sooner.
  32. It started as an angry protest after several new demos I downloaded
  33. refused to work on my 3000, and has ended up as a sort of general
  34. how-to-code type article, with particular emphasis on the Amiga 1200.
  35.  
  36. Now, as many of you may know, Commodore have not released
  37. hardware information on the AGA chipset, indeed they have said they
  38. will not (as the registers will change in the future). Demo coders
  39. may not be too concerned about what is coming in a year or two,
  40. but IF YOU ARE WRITING COMMERCIAL SOFTWARE you must be.
  41.  
  42. Chris Green, from Commodore US, asked me to mention the following:
  43.  
  44. "I'd like it if you acknowledged early in your text that it IS possible
  45. to do quite exciting demos without poking any hardware registers, and
  46. that this can be as interesting as direct hardware access.
  47. amiga.physik.unizh.ch has two AGA demos with source code by me, AABoing
  48. and TMapdemo. These probably seem pretty lame by normal demo standards
  49. as I didn't have time to do any nifty artwork or sound, and each only does
  50. one thing. but they do show the POTENTIAL for OS friendly demos."
  51.  
  52. I have seen these demos and they are very neat. Currently you
  53. cannot do serious copper tricks with the OS (or can you Chris? I'd
  54. love to see some examples if you can...), for example smooth
  55. gradiated background copperlists or all that fun messing with
  56. bitplane pointers and modulos. But for a lot of things the
  57. Kickstart 3.0 graphics.library is capable of lots. If you are
  58. in desperate need for some hardware trick that the OS can't handle,
  59. let Chris know about it, you never know what might make it into the
  60. next OS version!
  61.  
  62. Chris mentions QBlit and QBSBlit, interrupt driven blitter access.
  63. These are things that will make games in particular far easier
  64. to write under the OS now.
  65.  
  66. Chris also says "Note that if I did a 256 color lores screen using this
  67. document, it would run fifty times slower than one created using the OS,
  68. as you haven't figured out enhanced fetch modes yet. A Hires 256 color
  69. screen wouldn't even work."
  70.  
  71. There are some new additions to the AGA chapter that discuss some of
  72. this problem, but if you want maximum performance from an AGA system,
  73. use the OS.
  74.  
  75. Remember that on the A1200 chipram has wait-states, while the
  76. 32-bit ROM doesn't. So use the ROM routines, some of them run
  77. faster than anything you could possibly write (on a A1200 with
  78. just 2Mb ram).
  79.  
  80. The only drawback is again documentation. To learn how to code
  81. V39 OS programs you need the V39 includes and autodocs, which
  82. I'm not allowed to include here a) because I've signed an NDA,
  83. and b) because they're massive...
  84.  
  85. Perhaps, in a later release, I'll give some highlites of V39
  86. programming... Get Chris Green's example code, it's a good
  87. place to start.
  88.  
  89. Register as a developer with your local Commodore office to get
  90. the autodocs and includes, it's relatively inexpensive (£85 per
  91. year in the UK)
  92.  
  93. ---
  94.  
  95. Most demos I've seen use similar startup code to that I was using back
  96. in 1988. Hey guys, wake up! The Amiga has changed quite a bit since
  97. then.
  98.  
  99. So. Here are some tips on what to do and what not to do:
  100.  
  101.  
  102. 1. RTFM.
  103. ========
  104.  
  105. Read the f'ing manuals. All of them. Borrow them off friends or from
  106. your local public library if you have to.
  107.  
  108. Read the "General Amiga Development Guidelines" in the dark grey (2.04)
  109. Hardware Reference Manual and follow them TO THE LETTER.
  110. If it says "Leave this bit cleared" then don't set it!
  111.  
  112. Don't use self-modifying code. A common bit of code I see is:
  113.  
  114. ... in the setup code
  115.  
  116.         move.l  $6c.w,old                ; Store Level 3 interrupt.
  117.                                          ; Naughty... Naughty.
  118.  
  119. ..  at the end of the interrupt
  120.  
  121.         movem.l (sp)+,a0-a6/d0-d7
  122.         dc.w    $4ef9               ; jmp instruction
  123. old     dc.l    0                   ; self modifying!!!!
  124.  
  125. DONT DO THIS!
  126.  
  127. 68020 and above processors with cache enabled often barf at this
  128. piece of code (the cache still contains the JMP 0 instruction
  129. which isn't then altered).
  130.  
  131. Interrupts should be set up with the AddIntServer(), SetIntVector()
  132. or AddIntHandler() functions. Read the chapter on Interrupts in
  133. the Amiga Rom Kernal Manual: Libraries
  134.  
  135.  
  136.  
  137. 2. Proper Copper startup.
  138. =========================
  139.  
  140. (Please look at the startup example code at the end of this file).
  141.  
  142. IF you are going to use the copper then this is how you should set it
  143. up. The current workbench view and copper address are stored, and
  144. then the copper enabled. On exit the workbench view is restored.
  145.  
  146. This guarantees(*) your demo will run on an AGA (Amiga 1200) machine,
  147. even if set into some weird screen mode before running your code.
  148.  
  149. Otherwise under AGA, the hardware registers can be in some strange states
  150. before your code runs, beware!
  151.  
  152. The LoadView(NULL) forces the display to a standard, empty position,
  153. flushing the rubbish out of the hardware registers: Note. There is
  154. a bug in the V39 OS on Amiga 1200/4000 and the sprite resolution is
  155. *not* reset, you will have to do this manually if you use sprites
  156. (See below...)
  157.  
  158. Two WaitTOF() calls are needed after the LoadView to wait for both the
  159. long and short frame copperlists of interlaced displays to finish.
  160.  
  161. See the bottom of this file for a full, tested, example startup.asm
  162. code, that you can freely use for your own productions.
  163.  
  164. It has been suggested to me that instead of using the GfxBase gb_ActiView
  165. I should instead use the Intuition ib_ViewLord view. This will work
  166. just as well, but there has been debate as to whether in the future
  167. with retargetable graphics (RTG) this will work in the same way. As the
  168. GfxBase is at a lower level than Intuition, I prefer to access it this
  169. way (but thank's for the suggestion Boerge anyway!). Using gb_ActiView
  170. code should run from non-Workbench environments (for example, being
  171. called from within Amos) too...
  172.  
  173.  
  174. * - Nothing is ever guaranteed where Commodore are involved. They
  175. may move the hardware registers into chipram next week :-)
  176.  
  177.  
  178. 3. Your code won't run from an icon.
  179. ====================================
  180.  
  181. You stick an icon for your new demo (not everyone uses the CLI!) and
  182. it either crashes or doesn't give back all the RAM it uses. Why?
  183.  
  184. Icon startup needs specific code to reply to the workbench message.
  185. With the excellent Hisoft Devpac assember, all you need to do is add
  186. the line
  187.  
  188.     include "misc/easystart.i"
  189.  
  190. and it magically works!
  191.  
  192. For those without Devpac, here is the relevent code:
  193.  
  194. ---------------------------------------------------------
  195.  
  196. * Include this at the front of your program
  197. * after any other includes
  198. * note that this needs exec/exec_lib.i
  199.  
  200.     IFND    EXEC_EXEC_I
  201.     include    "exec/exec.i"
  202.     ENDC
  203.     IFND    LIBRARIES_DOSEXTENS_I
  204.     include    "libraries/dosextens.i
  205.     ENDC
  206.  
  207.  
  208.     movem.l    d0/a0,-(sp)        save initial values
  209.     clr.l    returnMsg
  210.  
  211.     sub.l    a1,a1
  212.     move.l    4.w,a6
  213.     jsr    _LVOFindTask(a6)        find us
  214.     move.l    d0,a4
  215.  
  216.     tst.l    pr_CLI(a4)
  217.     beq.s    fromWorkbench
  218.  
  219. * we were called from the CLI
  220.     movem.l    (sp)+,d0/a0        restore regs
  221.     bra    end_startup        and run the user prog
  222.  
  223. * we were called from the Workbench
  224. fromWorkbench
  225.     lea    pr_MsgPort(a4),a0
  226.     move.l    4.w,a6
  227.     jsr    _LVOWaitPort(A6)    wait for a message
  228.     lea    pr_MsgPort(a4),a0
  229.     jsr    _LVOGetMsg(A6)        then get it
  230.     move.l    d0,returnMsg        save it for later reply
  231.  
  232. * do some other stuff here RSN like the command line etc
  233.     nop
  234.  
  235.     movem.l    (sp)+,d0/a0        restore
  236. end_startup
  237.     bsr.s    _main            call our program
  238.  
  239. * returns to here with exit code in d0
  240.     move.l    d0,-(sp)        save it
  241.  
  242.     tst.l    returnMsg
  243.     beq.s    exitToDOS        if I was a CLI
  244.  
  245.     move.l    4.w,a6
  246.         jsr    _LVOForbid(a6)
  247.  
  248.     move.l    returnMsg(pc),a1
  249.     jsr    _LVOReplyMsg(a6)
  250.  
  251. exitToDOS
  252.     move.l    (sp)+,d0        exit code
  253.     rts
  254.  
  255. * startup code variable
  256. returnMsg    dc.l    0
  257.  
  258. * the program starts here
  259.     even
  260. _main
  261.  
  262. ---------------------------------------------------------
  263.  
  264.  
  265.  
  266.  
  267. 4. How do I tell if I'm running on an Amiga 1200/4000?
  268. ======================================================
  269.  
  270. Do *NOT* check library revision numbers, V39 OS can and does
  271. run on standard & ECS chipset machines (This Amiga 3000
  272. is currently running V39).
  273.  
  274. This code is a much better check for AGA than in the last
  275. issue!!!!!
  276.  
  277.  
  278. GFXB_AA_ALICE equ 2
  279. gb_ChipRevBits0 equ $ec
  280.  
  281. ; Call with a6 containing GfxBase from opened graphics.library
  282.  
  283.     btst    #GFXB_AA_ALICE,gb_ChipRevBits0(a6)
  284.     bne.s    is_aa
  285.  
  286. Chris Green pointed this out to me. He says quite rightly that the
  287. $dff07c register bits mentioned last time may very well change
  288. if the chip design is changed, even for new production models of the
  289. AA chipset. Thanks!
  290.  
  291. This will not work unless the V39 SetPatch command has been
  292. executed, so forget about Trackloader demos (and I wish you would!
  293. Some of us want to put your demos on our hard disk). Remember you
  294. can use Fast File System and Directory Caching System floppy disks
  295. on the A1200.
  296.  
  297. The code in the last issue also had major problems when being
  298. run on non ECS machines (without Super Denise or Lisa), as the
  299. register was undefined under the original (A) chipset, and
  300. would return garbage, sometimes triggering a false AGA-present
  301. response.
  302.  
  303.  
  304. 5. Use Relocatable Code
  305. =======================
  306.  
  307. If you write demos that run from a fixed address you should be shot.
  308. NEVER EVER DO THIS. It's stupid and completely unnecessary.
  309. Now with so many versions of the OS, different processors, memory
  310. configurations and third party peripherals it's impossible to
  311. say any particular area of ram will be free to just take and
  312. use.
  313.  
  314. It's not as though allocating ram legally is dificult. If you
  315. can't handle it then perhaps you should give up coding and
  316. take up graphics or something :-)
  317.  
  318. If you require bitplanes to be on a 64Kb boundary then try the
  319. following (in pseudo-code because I'm still too lazy to write it
  320. in asm for you):
  321.  
  322.         for c=65536 to (top of chip ram) step 65536
  323.  
  324.         if AllocAbs(c,NUMBER_OF_BYTES_YOU_WANT) == TRUE then goto ok:
  325.  
  326.     next c:
  327.  
  328.         print "sorry. No free ram. Close down something and retry demo!"
  329.         stop
  330.  
  331. ok:    Run_Outrageous_demo with mem at c
  332.  
  333. Keep your code in multiple sections. Several small sections are
  334. better than one large section, they will more easily fit in and run
  335. on a system with fragmented memory. Lots of calls across sections
  336. are slower than with a single section, so keep all your relevent
  337. code together. Keep code in a public memory section:
  338.  
  339.         section mycode,code
  340.  
  341. Keep graphics, copperlists and similar in a chip ram section:
  342.  
  343.         section mydata,data_c
  344.  
  345. Never use code_f,data_f or bss_f as these will fail on a chipram
  346. only machine.
  347.  
  348. And one final thing, I think many demo coders have realised this
  349. now, but $C00000 memory does not exist on any production machines
  350. now, so stop using it!!!
  351.  
  352.  
  353.  
  354.  
  355. 6. Don't Crunch demos!
  356. ======================
  357.  
  358. Don't ever use Tetrapack or Bytekiller based packers. They are crap.
  359. Many more demos fall over due to being packed with crap packers than
  360. anything else. If you are spreading your demo by electronic means
  361. (which most people do now, the days of the SAE Demodisks are long
  362. gone!) then assemble your code, and use LHARC to archive it, you
  363. will get better compression with LHARC than with most runtime
  364. packers.
  365.  
  366. If you *have* to pack your demos, then use Powerpacker 4+, Turbo
  367. Imploder or Titanics Cruncher, which I've had no problems with myself,
  368. although I have heard of problems with some of these on 68040 machines.
  369. If it will decrunch on a 68040 with caches enabled it will probably
  370. work on everything.
  371.  
  372. (found in the documentation to IMPLODER 4.0)
  373.  
  374. >** 68040 Cache Coherency **
  375. >
  376. >With the advent of the 68040 processor, programs that diddle with code which is
  377. >subsequently executed will be prone to some problems. I don't mean the usual
  378. >self-modifying code causing the code cached in the data cache to no longer
  379. >be as the algorithm expects. This is something the Imploder never had a
  380. >problem with, indeed the Imploder has always worked fine with anything
  381. >upto and including an 68030.
  382. >
  383. >The reason the 68040 is different is that it has a "copyback" mode. In this
  384. >mode (which WILL be used by people because it increases speed dramatically)
  385. >writes get cached and aren't guaranteed to be written out to main memory
  386. >immediately. Thus 4 subsequent byte writes will require only one longword
  387. >main memory write access. Now you might have heard that the 68040 does
  388. >bus-snooping. The odd thing is that it doesn't snoop the internal cache
  389. >buses!
  390. >
  391. >Thus if you stuff some code into memory and try to execute it, chances are
  392. >some of it will still be in the data cache. The code cache won't know about
  393. >this and won't be notified when it caches from main memory those locations
  394. >which do not yet contain code still to be written out from the data caches.
  395. >This problem is amplified by the absolutely huge size of the caches.
  396. >
  397. >So programs that move code, like the explosion algorithms, need to do a
  398. >cache flush after being done. As of version 4.0, the appended decompression
  399. >algorithms as well as the explode.library flush the cache, but only onder OS
  400. >2.0. The reason for this is that only OS 2.0 has calls for cache-flushing.
  401. >
  402. >This is yet another reason not to distribute imploded programs; they might
  403. >just cross the path of a proud '40 owner still running under 1.3.
  404. >
  405. >It will be interesting to see how many other applications will run into
  406. >trouble once the '40 comes into common use among Amiga owners. The problem
  407. >explained above is something that could not have been easily anticipated
  408. >by developers. It is known that the startup code shipped with certain
  409. >compilers does copy bits of code, so it might very well be a large problem.
  410.  
  411.  
  412. Look at some new EXEC-functions to solve this problem:
  413.  
  414.   CacheClearU() and CacheControl()
  415.  
  416. Both functions are available with Kickstart 2.0 and above.
  417.  
  418. I strongly disadvise trying to 'protect' code by encrypting
  419. parts of it, it's very easy for your code to fail on >68000 if you
  420. do. What's the point anyway? Lamers will still use Action Replay
  421. to get at your code.
  422.  
  423. I never learnt anything by disassembling anyones demo. It's far
  424. more dificult to try and understand someone elses (uncommented)
  425. code than to write your own code from scratch.
  426.  
  427.  
  428. 7. Don't use the K-Seka assembler!
  429. ==================================
  430.  
  431. It's dead and buried. Get a life, get a real assembler. Hisoft Devpac
  432. is probably the best all-round assembler, although I use ArgAsm
  433. which is astonishingly fast. The same goes for hacked versions of
  434. Seka.
  435.  
  436. Is it any coincidence that almost every piece of really bad
  437. code I see is written with Seka? No, I don't think so :-)
  438.  
  439. When buying an assembler check the following:
  440.  
  441. 1. That it handles standard CBM style include files without
  442. alteration.
  443.  
  444. 2. That it allows multiple sections
  445.  
  446. 3. That it can create both executable and linkable code
  447.  
  448. 4. 68020+ support is a good idea.
  449.  
  450. Devpac 3.0 is probably the best all-round assembler at the moment.
  451. People on a tighter budget could do worse than look at the
  452. public domain A68K (It's much better than Seka!). I'd suggest
  453. using Cygnus Ed as your Text Editor.
  454.  
  455. 8. Don't use the hardware unless you have to!
  456. =============================================
  457.  
  458. This one is aimed particularly at utility authors. I've seen some
  459. *awfully* written utilities, for example (although I don't want
  460. to single them out as there are plenty of others) the Kefrens
  461. IFF converter.
  462.  
  463. There is NO REASON why this has to have it's own copperlist. A standard
  464. OS-friendly version opening it's own screen works perfectly (I
  465. still use the original SCA IFF-Converter), and multitasks properly.
  466.  
  467. If you want to write good utilities, learn C.
  468.  
  469.  
  470. 9. Beware bogus input falling through to Workbench
  471. ==================================================
  472.  
  473. If you keep multitasking enabled and run your own copperlist remember
  474. that any input (mouse clicks, key presses, etc) fall through to the
  475. workbench. The correct way to get around this is to add an input
  476. handler to the IDCMP food chain (see - you *do* have to read the
  477. other manuals!) at a high priority to grab all input events before
  478. workbench/cli can get to them. You can then use this for your
  479. keyboard handler too (no more $bfexxx peeking, PLEASE!!!)
  480.  
  481. Look at the sourcecode for Protracker for an excellent example of
  482. how to do the job properly. Well done Lars!
  483.  
  484.  
  485.  
  486. 10. Have fun!
  487. =============
  488.  
  489. Too many people out there (particularly the American OS-Lamic
  490. Fundamentalists) try to tell us that you should never program at a hardware
  491. level. If you're programming for fun, ignore them! But try and put
  492. a little thought into how your code will work on other machines,
  493. nothing annoys people more than downloading 400Kb of demo and then
  494. finding it blows up on their machines. I'm not naming any names, but
  495. there are quite a few groups who I have no intention of downloading
  496. their demos again because I know it's a waste of download. With
  497. the launch of the Amiga 1200 you cannot just write for 1.3 Amiga
  498. 500's any more.
  499.  
  500. I'd like to apologise to all Americans for blaming OS-Fundamentalism
  501. on them. I've since heard from *two* American hardware hackers.
  502.  
  503. :-)
  504.  
  505. I guess I ought to point out that 90% of my programs are now
  506. fully OS legal, although I am writing an AGA hardware-hacking
  507. demo for the 1200 now... Demo and Source available soon....
  508. As soon as I have finished that I am writing a fully OS AGA demo,
  509. because I HAVE SEEN THE LIGHT! SATAN MADE ME USE THAT HARDWARE
  510. MANUAL. I WILL NEVER POKE A REGISTER AGAIN, or something like
  511. that... As usual full demo *and* source will be uploaded.
  512. If anyone has any ideas of what I should do (and I'd also
  513. appreciate a nice short tracker module...) you know where
  514. to send them...
  515.  
  516.  
  517.  
  518. 11. Don't Publish Code you haven't checked!
  519. ===========================================
  520.  
  521. Thanks to Timo Rossi for spotting the stupid bug in my copper
  522. setup routine (using LOFList instead of copinit). Funnily enough
  523. my own setup routine uses the correct copinit code:
  524.  
  525. Please ignore the original file (howtocode[1|2|3|4].txt) and use this
  526. instead.
  527.  
  528. 12. Copper End
  529. ==============
  530.  
  531. I've remembered where this double copper end comes from:
  532.  
  533. The ArgAsm assembler has copper macros (CMOVE, CWAIT and CEND)
  534. built in, and the CEND macro deliberately leaves two copper
  535. END instructions, the manual states this is important
  536. for compatibility reasons..
  537.  
  538. Will whoever pinched my ArgAsm manual please return it? I bet
  539. it was you Alex..
  540.  
  541.  
  542. 13. Using a 68010 processor
  543. ===========================
  544.  
  545. The 68010 is a direct replacement for the 68000 chip, it can
  546. be fitted to the Amiga 500,500+,1500 and 2000 without any
  547. other alterations (I have been told it will not fit an A600).
  548.  
  549. The main benefit of the 68010 over the 68000 is the loop cache mode.
  550. Common 3 word loops like:
  551.  
  552.        moveq  #50,d0
  553. .lp        move.b (a0)+,(a1)+  ; one word
  554.            dbra   d0,.lp       ; two words
  555.  
  556. are recognised as loops and speed up dramatically on 68010.
  557.  
  558.  
  559. 14. Using the blitter.
  560. ======================
  561.  
  562. If you are using the blitter in your code and you are leaving the
  563. system intact (as you should) always use the graphics.library
  564. functions OwnBlitter() and DisownBlitter() to take control
  565. of the blitter. Remember to free it for system use, many system
  566. functions (including floppy disk data decoding) use the blitter.
  567.  
  568. OwnBlitter() does not trash any registers. I guess DisownBlitter()
  569. doesn't either, although Chris may well correct me on this.
  570.  
  571. Another big mistake I've seen is with blitter/processor timing.
  572.  
  573. Assuming that a particular routine will be slow enough that a blitter
  574. wait is not needed is silly. Always check for blitter finished, and
  575. wait if you need to.
  576.  
  577. Don't assume the blitter will always run at the same speed too. Think
  578. about how your code would run if the processor or blitter were running
  579. at 100 times the current speed. As long as you keep this in mind,
  580. you'll be in a better frame of mind for writing compatible code.
  581.  
  582. Another big source of blitter problems is using the blitter in interrupts.
  583.  
  584. Most demos do all processing in the interrupt, with only a
  585.  
  586. .wt       btst       #6,$bfe001        ; is left mouse button clicked?
  587.        bne.s   .wt
  588.  
  589. loop outside of the interrupt. However, some demos do stuff outside the
  590. interrupt too. Warning. If you use blitter in both your interrupt
  591. and your main code, (or for that matter if you use the blitter via the
  592. copper and also in your main code), you may have big problems....
  593.  
  594. Take this for example:
  595.  
  596.     lea       $dff000,a5
  597.     move.l    GfxBase,a6
  598.     jsr       _LVOWaitBlit(a6)
  599.     move.l    #-1,BLTAFWM(a5)        ; set FWM and LWM in one go
  600.     move.l    #source,BLTAPT(a5)
  601.     move.l    #dest,BLTDPT(a5)
  602.     move.w    #%100111110000,BLTCON0(a5)
  603.     move.w    #0,BLTCON1(a5)
  604.     move.w    #64*height+width/2,BLTSIZE(a5)    ; trigger blitter
  605.  
  606. There is *nothing* stopping an interrupt, or copper, triggering a
  607. blitter operation between the WaitBlit call and
  608. your final BLTSIZE blitter trigger. This can lead to total system blowup.
  609.  
  610. Code that may, by luck, work on standard speed machines may die horribly
  611. on faster processors due to timing differences causing this type of
  612. problem to occurr.
  613.  
  614. The safest way to avoid this is to keep all your blitter calls together,
  615. use the copper exclusively, or write a blitter-interrupt routine to
  616. do your blits for you.
  617.  
  618. Always use the graphics.library WaitBlit() routine for your
  619. end of blitter code. It does not change any registers, takes into
  620. account any revision of blitter chip and any unusual circumstances,
  621. and on an Amiga 1200 will execute faster (because in 32-bit ROM)
  622. than from chipram.
  623.  
  624.  
  625.  
  626. Another thing concerning blitter:
  627.  
  628. Instead of calculating your LF-bytes all the time you can do this
  629. instead
  630.  
  631. A    EQU    %11110000
  632. B    EQU    %11001100
  633. C    EQU    %10101010
  634.  
  635. So when you need an lf-byte you can just type:
  636.  
  637.     move.w    #(A!B)&C,d0
  638.  
  639.  
  640.  
  641.  
  642. 15 NTSC
  643. =======
  644.  
  645. As an European myself, I'm naturally biased agains the inferior video
  646. system, but even though the US & Canada have a relatively minor Amiga
  647. community compared with Europe (Sorry, it's true :-) we should still
  648. help them out, even though they've never done a PAL Video Toaster for
  649. us (sob!).
  650.  
  651. You have two options.
  652.  
  653. Firstly, you could write your code only to use the first 200 display
  654. lines, and leave a black border at the bottom. This annoys PAL owners,
  655. who rightly expect things to have a full display. It took long enough
  656. for European games writers to work out that PAL displays were better.
  657.  
  658. You could write code that automatically checked which system it is
  659. running on and ran the correct code accordingly:
  660.  
  661. (How to check: Note, this is probably not the officialy supported method,
  662. but so many weird things happen with new monitors on AGA machines that
  663. I prefer this method, it's simpler, and works under any Kickstart)
  664.  
  665.     move.l    4.w,a6          ; execbase
  666.     cmp.b    #50,PowerSupplyFrequency(a6)    ; 531(a6)
  667.     beq.s    .pal
  668.  
  669.         jmp    I'm NTSC (or more accurately, I'm running from 60Hz power)
  670. .pal    jmp    I'm PAL  (or I'm running from 50hz power).
  671.  
  672.  
  673. If people have already switched modes to PAL, or if they are running
  674. some weird software like the ICD Flicker Free Video Prefs thingy, then
  675. this completely ignores them, but that serves them right for trying
  676. to be clever :-)
  677.  
  678. Probably better would be to check VBlankFrequency(a6) [530(a6)]
  679. as well, if both are 60Hz then it's definately a NTSC machine. If
  680. one or more are 50Hz, then it's probably a better idea to run in PAL.
  681. VBlankFrequency can give all sorts of weird things on an AGA
  682. system (DblPal runs at 48Hz, for example).
  683.  
  684. Chris Green suggests checking GfxBase->DisplayFlags for PAL
  685. rather than what I do above.
  686.  
  687. Well, If Commodore had fixed the bug in Kickstart 1.3 that
  688. was reported to them while Kickstart 1.2 was in beta (that a
  689. PAL machine, especially with a Genlock, often fails to report
  690. that it is PAL) then I'd use it. They did fix it in 2.0 though
  691. (at last!) along with the "Oh I've got $200000 RAM. I guess that
  692. means the user wants *two* mouse pointers in the PAL area" bug.. :-)
  693.  
  694. So, for V1.2/1.3 do the PowerSupplyFrequency() check, on 2.04 or
  695. higher use GfxBase->DisplayFlags check as Chris suggests...
  696.  
  697. Under Kickstart 2.04 or greater, the Display Database can be accessed.
  698. Any program can enquire of the database what type of displays
  699. are available, so for example "I want a 50hz 15Khz PAL screen. Can
  700. I display it on this Amiga?" (Unfortunately it doesn't take
  701. an ASCII string like that, but it's not much more dificult). Of
  702. course many users will have the default monitor installed (PAL or
  703. NTSC) and not realise that they can have extra modes by dragging
  704. the monitor icon into their Monitors drawer, and of course
  705. this doesn't work on Kickstart 1.3 machines.
  706.  
  707. Now, if you want to force a machine into the other display system
  708. you need some magic pokes: Here you go (beware other bits in
  709. $dff1dc can do nasty things. One bit can reverese the polarity
  710. on the video sync, not to healthy for some monitors I've heard...)
  711.  
  712. To turn a NTSC system into PAL (50Hz)
  713.  
  714.     move.w    #32,$dff1dc        ; Magically PAL
  715.  
  716. To turn a PAL system into NTSC (60Hz)
  717.  
  718.     move.w    #0,$dff1dc        ; Magically NTSC
  719.  
  720. Remember: Not all displays can handle both display systems!
  721. Commdore 1084/1084S, Philips 8833/8852 and multisync monitors
  722. will, and very few US TV's will handle PAL signals.
  723.  
  724. It might be polite for PAL demos to ask NTSC users if they
  725. wish to switch to PAL (by the magic poke) or quit.
  726.  
  727. 16 Programming AGA hardware
  728. ===========================
  729.  
  730. **** WARNING ****
  731.  
  732. AGA Registers are temporary. They will change. Do not rely
  733. on this documentation. No programs written with this information
  734. can be officially endorsed or supported by Commodore. If this
  735. bothers you then stop reading now.
  736.  
  737. I've rewritten this again, because of big mistakes, things
  738. that weren't really necessary, and because no-one realy understood
  739. the original. Remember that for most things the OS provides a much
  740. better and easier way to access new screen modes, and the OS
  741. will be compatible with future chipsets, these registers will
  742. change!
  743.  
  744.  
  745. Bitplanes:
  746. Set 0 to 7 bitplanes as before in $dff100.
  747. Set 8 bitplanes by setting bit 4 of $dff100, bits 12 to 15 should be zero.
  748. (ooops. Big mistake last time!)
  749.  
  750. Colour Registers:
  751.  
  752. There are now 256 colour registers, all accessed through the original
  753. 32 registers
  754.  
  755. AGA works with 8 differents palettes of 32 colors each, re-using
  756. colour registers from $0180 to $01BE.
  757.  
  758. You can choose the palette you want to access via the bits 13 to 15 of
  759. register $0106
  760.  
  761.  
  762. bit 15 | bit 14 | bit 13 | Selected palette
  763. -------+--------+--------+------------------------------
  764.    0   |    0   |    0   | Palette 0 (color 0 to 31)
  765.    0   |    0   |    1   | Palette 1 (color 32 to 63)
  766.    0   |    1   |    0   | Palette 2 (color 64 to 95)
  767.    0   |    1   |    1   | Palette 3 (color 96 to 125)
  768.    1   |    0   |    0   | Palette 4 (color 128 to 159)
  769.    1   |    0   |    1   | Palette 5 (color 160 to 191)
  770.    1   |    1   |    0   | Palette 6 (color 192 to 223)
  771.    1   |    1   |    1   | Palette 7 (color 224 to 255)
  772.  
  773. To move a 24-bit colour value into a colour register requires
  774. two writes to the register:
  775.  
  776. First clear bit 9 of $dff106
  777. Move high nibbles of each colour component to colour registers
  778.  
  779. Then set bit 9 of $dff106
  780. Move low nibbles of each colour components to colour registers
  781.  
  782. For example, to change colour zero to the colour $123456
  783.  
  784.    dc.l $01060000
  785.    dc.l $01800135
  786.    dc.l $01060200
  787.    dc.l $01800246
  788.  
  789. Note: As soon as you start messing with $dff106 forget all your
  790. fancy multi-colours-per-line plasma tricks. The colour only
  791. gets updated at the end of the scanline. Bummer dudes...
  792.  
  793.  
  794. Sprites:
  795. To  change  the  resolution  of the sprite, just use bit 7 and 6 of
  796. register $0106
  797.  
  798. bit 7 | bit 6 | Resolution
  799. ------+-------+-----------
  800.   0   |   0   | Lowres        (140ns)
  801.   1   |   0   | Hires        (70ns)
  802.   0   |   1   | Lowres        (140ns)
  803.   1   |   1   | SuperHires    (35ns)
  804. --------------------------
  805.  
  806. (Now.. 70ns sprites may not be available unless the Interlace bit in
  807. BPLCON0 is set. Don't ask me why....
  808. There appears to be much more to this than just these two bits.
  809. It seems to depend on a lot of different things...)
  810.  
  811. For 32-bit and 64-bit wide sprites use bit 3 and 2 of register $01FC
  812. Sprite format (in particular the control words) vary for each width.
  813.  
  814. bit 3 | bit 2 | Wide        | Control Words
  815. ------+-------+-------------+----------------------------------
  816.   0   |   0   | 16 pixels   | 2 words (normal)
  817.   1   |   0   | 32 pixels   | 2 longwords
  818.   0   |   1   | 32 pixels   | 2 longwords
  819.   1   |   1   | 64 pixels   | 2 double long words (4 longwords)
  820. ---------------------------------------------------------------
  821. Wider sprites are not available under all conditions.
  822.  
  823. It  is possible to choose the color palette of the sprite.
  824. This is done with bits 7 and 4 of register $010C.
  825.  
  826. bit 7 | bit 6 | bit 5 | bit 4 | Starting color of the sprite's palette
  827. ------+-------+-------+-------+------------------------------------------
  828.   0   |   0   |   0   |   0   | $0180/palette 0 (coulor 0)
  829.   0   |   0   |   0   |   1   | $01A0/palette 0 (color 15)
  830.   0   |   0   |   1   |   0   | $0180/palette 1 (color 31)
  831.   0   |   0   |   1   |   1   | $01A0/palette 1 (color 47)
  832.   0   |   1   |   0   |   0   | $0180/palette 2 (color 63)
  833.   0   |   1   |   0   |   1   | $01A0/palette 2 (color 79)
  834.   0   |   1   |   1   |   0   | $0180/palette 3 (color 95)
  835.   0   |   1   |   1   |   1   | $01A0/palette 3 (color 111)
  836.   1   |   0   |   0   |   0   | $0180/palette 4 (color 127)
  837.   1   |   0   |   0   |   1   | $01A0/palette 4 (color 143)
  838.   1   |   0   |   1   |   0   | $0180/palette 5 (color 159)
  839.   1   |   0   |   1   |   1   | $01A0/palette 5 (color 175)
  840.   1   |   1   |   0   |   0   | $0180/palette 6 (color 191)
  841.   1   |   1   |   0   |   1   | $01A0/palette 6 (color 207)
  842.   1   |   1   |   1   |   0   | $0180/palette 7 (color 223)
  843.   1   |   1   |   1   |   1   | $01A0/palette 7 (color 239)
  844. -------------------------------------------------------------------------
  845.  
  846. Bitplanes, sprites and copperlists should be 64-bit aligned
  847. under AGA. Bitplanes should also only be multiples of 64-bits
  848. wide, so if you want an extra area on the side of your screen for
  849. smooth blitter scrolling it must be *8 bytes* wide, not two as normal.
  850.  
  851. For example:
  852.  
  853.       CNOP  0,8
  854. sprite   incbin "myspritedata"
  855.  
  856.       CNOP  0,8
  857. bitplane incbin "mybitplane"
  858.  
  859. and so on.
  860.  
  861. This also raises another problem. You can no longer use
  862. AllocMem() to allocate bitplane/sprite memory directly.
  863.  
  864. Either use AllocMem(sizeofplanes+8) and calculate how many
  865. bytes you have to skip at the front to give 64-bit alignment
  866. (remember this assumes either you allocate each bitplane
  867. individually or make sure the bitplane size is also an
  868. exact multiple of 64-bits), or you can use the new V39
  869. function AllocBitMap().
  870.  
  871.  
  872. 17. Keyboard Timings
  873. ====================
  874.  
  875. If you have to read the keyboard by hardware, be very careful
  876. with your timings. Not only do different processor speeds affect
  877. the keyboard timings (for example, in the game F-15 II Strike Eagle
  878. on an Amiga 3000 the key repeat delay is ridiculously short, you
  879. ttyyppee lliikkee tthhiiss aallll tthhee ttiimmee. You use
  880. up an awful lot of Sidewinders very quickly!), but there are differences
  881. between different makes of keyboard, some Amiga 2000's came with
  882. Cherry keyboards, these have small function keys the same
  883. size as normal alphanumeric keys - these keyboards have different
  884. timings to the normal Mitsumi keyboards.
  885.  
  886. Use an input handler to read the keyboard. The Commodore guys
  887. have spent ages writing code to handle all the different possible
  888. hardware combinations around, why waste time reinventing the wheel?
  889.  
  890. 18. How to break out of never-ending loops
  891. ==========================================
  892.  
  893. Another great tip for Boerge here:
  894.  
  895. >This is a simple tip I have. I needed to be able to break out of my
  896. >code if I had neverending loops. I also needed to call my exit code when I did
  897. >this. Therefore I could not just exit from the keyboard interrupt which I have
  898. >taken over(along with the rest of the machine). My solution wa to enter
  899. >supervisor mode before I start my program, and if I set the stack back then
  900. >I can do an RTE in the interrupt and just return from the Supervisor() call.
  901. >This is snap'ed from my code:
  902. >
  903. >    lea     .SupervisorCode,a5
  904. >    move.l  sp,a4           ;
  905. >    move.l  (sp),a3         ;
  906. >    EXEC    Supervisor
  907. >    bra     ReturnWithOS
  908. >
  909. >.SupervisorCode
  910. >    move.l  sp,crashstack   ; remember SSP
  911. >    move.l  USP,a7          ; swap USP and SSP
  912. >    move.l  a3,-(sp)        ; push return address on stack
  913. >
  914. >that last was needed because it was a subroutine that RTSes (boy did I have
  915. >porblems working out my crashes before I fixed that)
  916. >Then I have my exit code:
  917. >
  918. >ReturnWithOS
  919. >    tst.l   crashstack
  920. >    beq     .nocrash
  921. >    move.l  crashstack,sp
  922. >    clr.l   crashstack
  923. >    RTE                     ; return from supervisor mode
  924. >.nocrash
  925. >
  926. >my exit code goes on after this.
  927. >
  928. >This made it possible to escape from an interrupt without having to care
  929. >for what the exception frames look like.
  930.  
  931. I haven't tried this because my code never crashes. ;-)
  932.  
  933.  
  934. 19. Version numbers!
  935. ====================
  936.  
  937. Put version numbers in your code. This allows the CLI version command
  938. to determine easily the version of both your source and executable
  939. files. Some directory utilities allow version number checking too (so
  940. you can't accidentally copy a newer version of your source over
  941. an older one, for example). Of course, if you pack your files the
  942. version numbers get hidden. Leaving version numbers unpacked
  943. was going to be added to PowerPacker, but I don't know if this is
  944. done yet.
  945.  
  946. A version number string is in the format
  947.  
  948. $VER: howtocode5.txt 5.0 (18.03.92)
  949. ^          ^          ^Version number (date is optional)
  950. |          |
  951. |          | File Name
  952. |
  953. | Identifier
  954.  
  955. The Version command searches for $VER and prints the string it finds
  956. following it.
  957.  
  958. For example, adding the line to the begining of your source file
  959.  
  960. ; $VER: MyFunDemo.s 4.0 (01.01.93)
  961.  
  962. and somewhere in your code
  963.  
  964.     dc.b    "$VER: MyFunDemo 4.0 (01.01.93)",0
  965.  
  966. means if you do VERSION MyFunDemo.s you will get:
  967.  
  968. MyFunDemo.s 4.0 (01.01.93)
  969.  
  970. and if you assemble and do Version MyFunDemo, you'll get
  971.  
  972. MyFunDemo 4.0 (01.01.93)
  973.  
  974. Try doing version howtocode5.txt and see what you get :-)
  975.  
  976. This can be very useful for those stupid demo compilations
  977. where everything gets renamed to 1, 2, 3, etc...
  978.  
  979. Just do version 1 to get the full filename (and real date)
  980.  
  981. Does this work on Kickstart 1.3? I can't remember, I ditched
  982. my 1.3 Kickstart 2 years ago :-)
  983.  
  984.  
  985. 20. CDTV
  986. ========
  987.  
  988. I've been asked if there is any special advice on how to program
  989. demos to work on CDTV, and if hardware access to the CDTV (for
  990. playing CD Audio, etc) is possible.
  991.  
  992. The CDTV is essentially a 1Mb chip ram Amiga with a CD-ROM drive.
  993. The major difference (apart from lack of fast ram or $c00000 ram)
  994. is that the CDTV roms can take up anything from 100-200Kb of ram.
  995.  
  996. Many demos fail on CDTV through lack of memory.
  997.  
  998. You can hack your CDTV to switch on/off these roms (put a switch
  999. on JP15), when switched off the CDTV has a full 1Mb of memory and
  1000. more software works, but you can still play audio CD's in the CD
  1001. drive..
  1002.  
  1003. I have no information on how to program the CDTV at the hardware
  1004. level. Currently the only supported way to access the CDTV
  1005. special functions is by the CDTV.DEVICE, a standard ROM device
  1006. that can be OpenDevice()d and sent IORequests. I don't think
  1007. I'm allowed to give out the documentation for this, sorry :-(
  1008.  
  1009. 21. Copper Wait Commands
  1010. ========================
  1011.  
  1012. The Hardware Reference manual states a copper wait for the start
  1013. of line xx is done with:
  1014.  
  1015. $xx01,$fffe
  1016.  
  1017. However (as many of you have found out), this actually triggers
  1018. just before the end of the previous line (around 4 or 5 low-res
  1019. pixels in from the maximum overscan border).
  1020.  
  1021. For most operations this is not a problem (and indeed gives a little
  1022. extra time to initialise stuff for the next line), but if you are
  1023. changing the background colour ($dff180), then there is a noticable
  1024. 'step' at the end of the scanline.
  1025.  
  1026. The correct way to do a copper wait to avoid this problem is
  1027.  
  1028. $xx07,$fffe.
  1029.  
  1030. This just misses the previous scanline, so the background colour is
  1031. changed exactly at the start of the scanline, not before.
  1032.  
  1033.  
  1034.  
  1035. 22. Screen Modulos (thanks Magnus for this one...)
  1036. ==================================================
  1037.  
  1038. Don't assume bitplane modulos (BPL0MOD and BPL1MOD) will be
  1039. set to zero. If you require zero modulos set them, at the start
  1040. of your copperlist is as good a place as any.
  1041.  
  1042. Under V39 OS the workbench is interleaved by default, so the
  1043. modulo can be huge...
  1044.  
  1045. Indeed, do not assume that *any* hardware register is set
  1046. to a particular value.
  1047.  
  1048.  
  1049.  
  1050. 23. Open Graphics Library! (Thanks Magnus, CS, and others..)
  1051. ============================================================
  1052.  
  1053. I've never seen this in use before, but Magnus spotted
  1054. it. It's got to be one of the worst pieces of code I've
  1055. ever seen! Don't ever do this!
  1056.  
  1057.     move.l    4.w,a0          ; get execbase
  1058.     move.l    (a0),a0         ; wandering down the library list...
  1059.     move.l    (a0),a0     ; right. I think this is graphics.library
  1060.  
  1061.     ; now goes ahead and uses a0 as gfxbase...
  1062.  
  1063. Oh yes, graphics.library is always going to be second down the chain from
  1064. Execbase?
  1065.  
  1066. If you want to access gfxbase (or any other library base) OPEN the
  1067. library. Do not wander down the library chain, either by guesswork or
  1068. by manually checking for "graphics.library" in the library base name.
  1069. OpenLibrary() will do this for you.
  1070.  
  1071. Here is the only official way to open a library.
  1072.  
  1073.     MOVEA.L    4,a6
  1074.     LEA.L    gfxname(PC),a1
  1075.     MOVE.L    #39,d0            ; version required (here V39)
  1076.     JSR    _LVOOpenLibrary(a6)    ; resolved by linking with amiga.lib
  1077.                     ; or by include "exec/exec_lib.i"
  1078.     TST.L    d0
  1079.     BEQ.S    OpenFailed
  1080.     ; use the base value in d0 as the a6 for calling graphics functions
  1081.     ; remember d0/d1/a0/a1 are scratch registers for system calls
  1082.  
  1083. gfxname    DC.B    'graphics.library',0
  1084.  
  1085. Don't use OldOpenLibrary! Always open libraries with a version, at least V33.
  1086. V33 is equal to Kickstart 1.2. And DON'T forget to check the result returned
  1087. in d0 (and nothing else).
  1088.  
  1089.  
  1090. 24. Protracker Replay code bug
  1091. ==============================
  1092.  
  1093. I've just got the Protracker 2.3 update, and the replay code (both
  1094. the VBlank and CIA code) still has the same bug from 1.0!
  1095.  
  1096. At the front of the file is an equate
  1097.  
  1098. >DMAWait = 300 ; Set this as low as possible without losing low notes.
  1099.  
  1100. And then it goes on to use 300 as a hard coded value, never refering
  1101. to DMAWait!
  1102.  
  1103. Now, until I can get some free time to write a reliable scanline-wait
  1104. routine to replace their DBRA loops (does anyone want to write a better
  1105. Protracker player? Free fame & publicity :-), I suggest you change
  1106. the references to 300 in the code (except in the data tables!) to
  1107. DMAWait, and you make the DMAWait value *MUCH* higher.
  1108.  
  1109. I use 1024 on this Amiga 3000 without any apparent problem, but
  1110. perhaps it's safer to use a value around 2000. Has anyone tried
  1111. Protracker on a 68040 machine, if so, what DMAWait value in Prefs
  1112. is needed to make all modules sound ok?
  1113.  
  1114. Or, does anyone have a system friendly version of the ProRunner
  1115. replay? The one I have is awful, it hits the CIA timer hardware
  1116. directly so nothing can use the CIA's once it quits.
  1117.  
  1118. 25. Devpac optimise mode produces crap code?
  1119. ============================================
  1120.  
  1121. If you're using Devpac and have found that the OPT o+ flag produces
  1122. crap code, then you need to add the option o3-. I can't remember
  1123. what this option does, my Devpac 3 manual is at the office.
  1124.  
  1125. 26. Argasm produces crap code, whatever happens
  1126. ===============================================
  1127.  
  1128. First, Argasm (unlike Devpac) from the Command Line or if called from
  1129. Arexx using Cygnus Ed (my prefered system) defaults to writing linkable
  1130. code, so you need to add
  1131.  
  1132.         opt    l-        (disable linkable code)
  1133.  
  1134. If you find that your Argasm executables fail then check you haven't
  1135. got any BSR's across sections! Argasm seems to allow this, but of
  1136. course the code doesn't work. Jez San from Argonaut software who
  1137. publish ArgAsm says it's not a bug, but a feature of the linker...
  1138.  
  1139. Yeah right Jez...
  1140.  
  1141. But Argasm is *fast*, and it produces non-working code
  1142. *faster* than any other assembler :-)
  1143.  
  1144. I still use it though, but Devpac comes in handy for checking
  1145. code every now and then. Argonaut have abandoned ArgAsm so
  1146. the last version (1.09d) is the last. There will be no more...
  1147.  
  1148.  
  1149. 27. Help! I'm starting to code in assembler. Where do I begin?
  1150. ==============================================================
  1151.  
  1152. If you are just starting to learn programming, and you want
  1153. a good place to begin learning assembler, buy Amos!. It's
  1154. very easy to write assembler code, load it into amos and test it.
  1155.  
  1156. For example, take this routine:
  1157.  
  1158. ;simplemaths.s
  1159.  
  1160.         add.l    d0,d1        ; add contents of d0 to d1
  1161.         rts
  1162.  
  1163. Assemble this with Devpac and what do you get? Not a lot.
  1164.  
  1165. Now, load AMOS and type this:
  1166.  
  1167. Pload "ram:simplemaths",1  ' load executable file into bank 1
  1168. Input "Enter a number ";n1
  1169. Input "Enter another number ";n2
  1170. dreg(0) = n1               ' Store n1 in 68000 register d0
  1171. dreg(1) = n2               ' Store n2 in 68000 register d1
  1172. call(1)                    ' Run your machinecode routine
  1173. Print n1;" plus ";n2;" equals ";dreg(1)    ' returns result in d1
  1174.  
  1175. You can start playing with 68000 instructions this way, seeing how
  1176. they work, without having to 'jump in the deep end' writing
  1177. routines to set up displays, copperlists, windows or writing to
  1178. the console.
  1179.  
  1180. You can also pass your machine code the address of AMOS's
  1181. bitplanes (by Phybase(0) to Phybase(n) where n is number of
  1182. bitplanes - 1), so you can write your own vector/bob code
  1183. and test it easily before writing your own front end code.
  1184.  
  1185. Once you have got the hang of 68000, you can drop Amos.
  1186.  
  1187.  
  1188. Another good way is to write some code in C, and use the inline
  1189. debugging options with SAS C, and OMD to examine what your C
  1190. compiler actually generates. To do this with SAS V6.x do the
  1191. following
  1192.  
  1193. SC debug=full myprog.c
  1194.  
  1195. OMD >ram:omdoutput myprog.o myprog.c
  1196.  
  1197. You will get each line of C code interleaved with the assembler
  1198. that it generates. Very handy!
  1199.  
  1200. It's also amazing how good the code generated by SAS C 6.2 really
  1201. is.
  1202.  
  1203. 28 How can I tell what processor I am running on?
  1204. =================================================
  1205.  
  1206. Look inside your case. Find the large rectangular (or Square) chip,
  1207. read the label :-)
  1208.  
  1209. Or...
  1210.  
  1211.     move.l  4.w,a6
  1212.     move.w  AttnFlags(a6),d0    ; get processor flags
  1213.  
  1214. d0.w is then a bit array which contains the following bits
  1215.  
  1216. Bit    Meaning if set
  1217.  
  1218. 0  68010 processor fitted (or 68020/30/40)
  1219. 1  68020 processor fitted (or 68030/40)
  1220. 2  68030 processor fitted (or 68040)   [V37+]
  1221. 3  68040 processor fitted              [V37+]
  1222. 4  68881 FPU fitted       (or 68882)
  1223. 5  68882 FPU fitted                    [V37+]
  1224. 6  68040 FPU fitted                    [V37+]
  1225.  
  1226.  
  1227. The 68040 FPU bit is set when a working 68040 FPU
  1228. is in the system.  If this bit is set and both the
  1229. 68881 and 68882 bits are not set, then the 68040
  1230. math emulation code has not been loaded and only 68040
  1231. FPU instructions are available.  This bit is valid *ONLY*
  1232. if the 68040 bit is set.
  1233.  
  1234. Don't forget to check which ROM version you're running.
  1235.  
  1236. DO NOT assume that the system has a >68000 if the word is non-zero!
  1237. 68881 chips are available on add-on boards without any faster processor.
  1238.  
  1239. And don't assume that a 68000 processor means a 7Mhz 68000. It may well
  1240. be a 14Mhz processor.
  1241.  
  1242. So, you can use this to determine whether specific processor functions
  1243. are available (more on 68020 commands in a later issue), but *NOT*
  1244. to determine values for timing loops. Who knows, Motorola may
  1245. release a 100Mhz 68020 next year  :-)
  1246.  
  1247. Does anyone know a system-friendly way to check for MMU?
  1248.  
  1249.  
  1250. 29. All addresses are 32 bit
  1251. ============================
  1252.  
  1253. "Oh look" says clever programmer. "If I access $dcdff180 I can access
  1254. the colour0 hardware register, but it confuses people hacking my
  1255. code!".
  1256.  
  1257. Oh no you can't. On a machine with a 32-bit address bus (any
  1258. accelerated Amiga) this doesn't work. And all us hackers know this
  1259. trick now anyway :-)
  1260.  
  1261. Always pad out 24-bit addresses (eg $123456) with ZEROs in the high
  1262. byte ($00123456). Do not use the upper byte for data, for storing
  1263. your IQ, for scrolly messages or for anything else.
  1264.  
  1265. Similarly, on non ECS machines the bottom 512k of memory was paged
  1266. four times on the address bus, eg:
  1267.  
  1268.     move.l #$12345678,$0
  1269.  
  1270.     move.l    $80000,d0    ; d0 = $12345678
  1271.     move.l    $100000,d1    ; d1 = $12345678
  1272.     move.l    $180000,d2    ; d2 = $12345678
  1273.  
  1274. This does not work on ECS and upwards!!!! You will get meaningless
  1275. results if you try this, so PLEASE do not do it!
  1276.  
  1277.  
  1278. 30. Action Replay Cartridges
  1279. ============================
  1280.  
  1281. These things are great fun, even more so if you get into the
  1282. 'sysop mode' (Allows disassembly of ram areas not previously
  1283. allowed by Action Replay, including non-autoconfig ram and
  1284. the cartridge rom!)
  1285.  
  1286. To get into sysop mode on Action Replay 1 type:
  1287.  
  1288. LORD OLAF
  1289.  
  1290. To get into sysop mode on Action Replay 2 type:
  1291.  
  1292. MAY
  1293. THE
  1294. FORCE
  1295. BE
  1296. WITH
  1297. YOU
  1298.  
  1299. To get into sysop mode on Action Replay 3 type the same as
  1300. Action Replay 2. After this you get a message
  1301. "Try a new one".
  1302. Then type in
  1303.  
  1304. NEW
  1305.  
  1306. and sysop powers are granted!
  1307.  
  1308.  
  1309. 31. Avoiding Forbid() and Permit()
  1310. ==================================
  1311.  
  1312. I've tried it, this works, it's wonderful.
  1313.  
  1314. Instead of using Forbid() and Permit() to prevent the OS stealing
  1315. time from your code, you could put your demo or game at a high
  1316. task priority.
  1317.  
  1318. The following code at the beginning will do this:
  1319.  
  1320.  
  1321.         move.l  4.w,a6
  1322.         sub.l   a1,a1            ; Zero - Find current task
  1323.         jsr     _LVOFindTask(a6)
  1324.  
  1325.         move.l  d0,a1
  1326.         moveq   #127,d0         ; task priority to very high...
  1327.         jsr     _LVOSetTaskPri(a6)
  1328.  
  1329. Now, only essential system activity will dare to steal time
  1330. from your code. This means you can now carry on using dos.library
  1331. to load files from hard drives, CD-ROM, etc, while your code
  1332. is running.
  1333.  
  1334. Try using this instead of Forbid() and Permit(), and insert a new
  1335. floppy disk while your code is running. Wow... The system
  1336. recognises the disk change....  But remember to add your
  1337. input handler!!!
  1338.  
  1339. Of course this is purely up to you. You may prefer to Forbid() when
  1340. your code is running (it makes it easier to write).
  1341.  
  1342. Several people have suggested to me that I needed to do a Forbid()
  1343. *before* the LoadView(NULL);WaitTOF();WaitTOF(); code, in case something
  1344. else has run and opened a display (disrupting copper registers) in
  1345. the meantime.
  1346.  
  1347. There is no point doing this because WaitTOF() disables the Forbid()
  1348. state anyway... Ok..  you could write a busy-loop to check for VBlank,
  1349. but it's much better to specifically check if a view has opened,
  1350. check if gb_ActiView is not zero. If it's zero, it's ok to carry on,
  1351. otherwise LoadView(NULL);WaitTOF();WaitTOF() again, and so on...
  1352.  
  1353. Now... I haven't actually checked this, I haven't had time, but
  1354. it should work! :-)   (I'll live to regret this, I know...)
  1355.  
  1356. 32. 68020 Optimization (Thanks Chris)
  1357. =====================================
  1358.  
  1359. A1200 speed issues:
  1360.  
  1361. The A1200 has a fairly large number of wait-states when accessing
  1362. chip-ram. ROM is zero wait-states. Due to the slow RAM speed, it may be
  1363. better to use calculations for some things that you might have used tables
  1364. for on the A500.
  1365.  
  1366. Add-on RAM will probably be faster than chip-ram, so it is worth
  1367. segmenting your game so that parts of it can go into fast-ram if available.
  1368.  
  1369. For good performance, it is critical that you code your important loops
  1370. to execute entirely from the on-chip 256-byte cache. A straight line loop
  1371. 258 bytes long will execute far slower than a 254 byte one.
  1372.  
  1373. The '020 is a 32 bit chip. Longword accesses will be twice as fast when
  1374. they are aligned on a long-word boundary. Aligning the entry points of
  1375. routines on 32 bit boundaries can help, also. You should also make sure
  1376. that the stack is always long-word aligned.
  1377.  
  1378. Write-accesses to chip-ram incur wait-states. However, other processor
  1379. instructions can execute while results are being written to memory:
  1380.  
  1381.     move.l    d0,(a0)+    ; store x coordinate
  1382.     move.l    d1,(a0)+    ; store y coordinate
  1383.     add.l    d2,d0        ; x+=deltax
  1384.     add.l    d3,d1        ; y+=deltay
  1385.  
  1386.     will be slower than:
  1387.  
  1388.     move.l    d0,(a0)+    ; store x coordinate
  1389.     add.l    d2,d0        ; x+=deltax
  1390.     move.l    d1,(a0)+    ; store y coordinate
  1391.     add.l    d3,d1        ; y+=deltay
  1392.  
  1393. The 68020 adds a number of enhancements to the 68000 architecture,
  1394. including new addressing modes and instructions. Some of these are
  1395. unconditional speedups, while others only sometimes help:
  1396.  
  1397.     Adressing modes:
  1398.  
  1399. o   Scaled Indexing. The 68000 addressing mode (disp,An,Dn) can have
  1400.     a scale factor of 2,4,or 8 applied to the data register on the 68020.
  1401.     This is totally free in terms of instruction length and execution time.
  1402.     An example is:
  1403.  
  1404.         68000                   68020
  1405.         -----                   -----
  1406.         add.w   d0,d0           move.w  (0,a1,d0.w*2),d1
  1407.         move.w  (0,a1,d0.w),d1
  1408.  
  1409. o   16 bit offsets on An+Rn modes. The 68000 only supported 8 bit
  1410.     displacements when using the sum of an address register and another
  1411.     register as a memory address. The 68020 supports 16 bit displacements.
  1412.     This costs one extra cycle when the instruction is not in cache, but is
  1413.     free if the instruction is in cache. 32 bit displacements can also be
  1414.     used, but they cost 4 additional clock cycles.
  1415.  
  1416. o   Data registers can be used as addresses. (d0) is 3 cycles slower than
  1417.     (a0), and it only takes 2 cycles to move a data register to an address
  1418.     register, but this can help in situations where there is not a free
  1419.     address register.
  1420.  
  1421. o   Memory indirect addressing. These instructions can help in some
  1422.     circumstances when there are not any free register to load a pointer
  1423.     into. Otherwise, they lose.
  1424.  
  1425.     New instructions:
  1426.  
  1427. o   Extended precision divide an multiply instructions. The 68020 can
  1428.     perform 32x32->32, 32x32->64 multiplication and 32/32 and 64/32
  1429.     division. These are significantly faster than the multi-precision
  1430.     operations which are required on the 68000.
  1431.  
  1432. o   EXTB. Sign extend byte to longword. Faster than the equivalent
  1433.     EXT.W EXT.L sequence on the 68000.
  1434.  
  1435. o   Compare immediate and TST work in program-counter relative mode
  1436.     on the 68020.
  1437.  
  1438. o   Bit field instructions. BFINS inserts a bitfield, and is faster
  1439.     than 2 MOVEs plus and AND and an OR. This instruction can be used
  1440.     nicely in fill routines or text plotting. BFEXTU/BFEXTS can extract
  1441.     and optionally sign-extend a bitfield on an arbitrary boundary.
  1442.     BFFFO can find the highest order bit set in a field. BFSET, BFCHG,
  1443.     and BFCLR can set, complement, or clear up to 32 bits at arbitrary
  1444.     boundaries.
  1445.  
  1446.  
  1447. o   On the 020, all shift instructions execute in the same amount of time,
  1448.     regardless of how many bits are shifted. Note that ASL and ASR are
  1449.     slower than LSL and LSR. The break-even point on ADD Dn,Dn versus LSL
  1450.     is at two shifts.
  1451.  
  1452. o   Many tradeoffs on the 020 are different than the 68000.
  1453.  
  1454. o   The 020 has PACK an UNPACK which can be useful.
  1455.  
  1456.  
  1457.  
  1458.  
  1459.  
  1460. 33. Sprites
  1461. ===========
  1462. Some people doesn't initialize the sprites they don't want to
  1463. use correctly. (This reminds me of Soundtracker.)
  1464. A common error is unwanted sprites pointing at address $0.
  1465. If the longword at address $0 isn't zero you'll get some funny looking
  1466. sprites at unpredictable places.
  1467.  
  1468. The right way of getting rid of sprites is to point them to an address
  1469. you for sure know is #$00000000 (0.l), and with AGA you may need to
  1470. point to FOUR long words of 0 on a 64-bit boundary
  1471.  
  1472.            CNOP 0,8
  1473. pointhere: dc.l    0,0,0,0
  1474.  
  1475. The second problem is people turning off the sprite DMA at the wrong time.
  1476. Vertical stripes on the screen are not always beautiful. Wrong time means
  1477. that you turn off the DMA when it is "drawing" a sprite.
  1478. It is very easy to avoid this.
  1479. Just turn off the DMA when the raster is in the vertical blank area.
  1480.  
  1481. Currently V39 Kickstart has a bug where sprite resolution and width
  1482. are not always reset when you run your own code. To reset this
  1483. you must do the following (but only if you detect AGA chipset)
  1484.  
  1485.     move.w    #0,$dff1fc
  1486.     move.w    #0,$dff106
  1487.  
  1488. Remember this will also zero the other bits in these registers,
  1489. so do this before any of your other setup!
  1490.  
  1491.  
  1492. 34. Trackloaders
  1493. ================
  1494. Use CIA timers! DON'T use processor timing. If you use processor timing you
  1495. will MESS UP the diskdrives in accelerated Amigas.
  1496.  
  1497. Use AddICRVector to allocate your timers, don't hit $bfxxxx
  1498. addresses!!!
  1499.  
  1500. On second thoughts. DON'T use trackloaders! Use Dos...
  1501.  
  1502.  
  1503.  
  1504. 35. Debug with Enforcer
  1505. =======================
  1506.  
  1507. Commodore have written a number of utilities that are *excellent*
  1508. for debugging. They are great for trapping errors in code, such
  1509. as illegal memory access and using memory not previously allocated.
  1510.  
  1511. The down side is they need to things:
  1512.  
  1513. a) A Memory Management Unit (at least for Enforcer). This rules
  1514. out any 68000 machine, and (unfortunately) the Amiga 1200 and the
  1515. Amiga 4000/EC030. If you are seriously into programming insist on
  1516. a FULL 68030/40 chip, accept no substitute. Amiga 2000 owners
  1517. on a tight budget may want to look at the Commodore A2620 card
  1518. (14Mhz 68020 with 68851 MMU fitted) which will work and is now
  1519. very cheap.
  1520.  
  1521. b) A serial terminal. This is really essential anyway, any
  1522. serious programmer will have a terminal (I have an old Amiga 500
  1523. running NCOMM for this task) to output debug information with dprintf()
  1524. from their code. This is the only sensible way to display debug info
  1525. while messing with copperlists and hardware displays.
  1526.  
  1527. Enforcer, Mungwall and other utilities are available on Fred Fish
  1528. Disks, amiga.physik and wuarchive, and probably on an issue of the
  1529. excellent "The Source" coders magazine from Epsilon.
  1530.  
  1531. 36. More Accurate Vector Maths
  1532. ==============================
  1533.  
  1534. A little (little) math hint for vector calculations:
  1535.  
  1536. When doing a muls with a value and then downshifting the value, use
  1537. and 'addx' to get roundoff error instead of truncated error, for
  1538. example:
  1539.     moveq    #0,d7
  1540. DoMtxMul
  1541.     .
  1542.     .
  1543.     muls    (a0),d0        ;Do a muls with a sin value *256
  1544.     asr.l    #8,d0
  1545.     addx.w    d7,d0        ;trunc > roundoff
  1546.     .
  1547.     .
  1548.  
  1549. When you do a 'asr' the last outshifted bit goes to the x-flag.
  1550. if you use an addx with source=0 => dest=dest+'x-flag'.
  1551. This halves the error, and makes complicated vector objects
  1552. less 'hacky'... Just an Idea ... And it don't take too many
  1553. cycles either...
  1554.  
  1555. Hope it helps.
  1556. /Carl-Henrik Sk}rstedt (Asterix - Movement)
  1557.  
  1558.  
  1559.  
  1560.  
  1561. 37. 68000 Optimization
  1562. ======================
  1563.  
  1564. ASSEMBLY CODE OPTIMIZATION (READ: "HOW AS FAST AND SMALL AS POSSIBLE?").
  1565. Written by Irmen de Jong, march '93. (E-mail: ijdjong@cs.vu.nl)
  1566.  
  1567. Some notes added by CJ
  1568.  
  1569. -----------------------------------------------------------------------------
  1570. Original    Possible optimization    Examples/notes
  1571. -----------------------------------------------------------------------------
  1572. STANDARD WELL-KNOWN OPTIMIZATIONS
  1573. RULE: use Quick-type/Short branch! Use INLINE subroutines if they are small!
  1574. -----------------------------------------------------------------------------
  1575.  
  1576. BRA/BSR    xx    BRA.s/BSR.s xx        if xx is close to PC
  1577.  
  1578. MOVE.X #0    CLR.X/MOVEQ/SUBA.X    move.l #0,count -> clr.l count
  1579.                     move.l #0,d0    -> moveq #0,d0
  1580.                     move.l #0,a0    -> sub.l a0,a0
  1581.  
  1582. CLR.L Dx    MOVEQ #0,Dx        -
  1583.  
  1584. CMP #0        TST            -
  1585.  
  1586. MOVE.L #nn,dx    MOVEQ #nn,dx        possible if -128<=nn<=127
  1587.  
  1588. ADD.X #nn    ADDQ.X #nn        possible if 1<=nn<=8
  1589. SUB.X #nn    SUBQ.X #nn        same...
  1590.  
  1591. JMP/JSR xx    BRA/BSR    xx        possible if xx is close to PC
  1592.                     * and in same section!*
  1593.                     (what's the use of JMP/JSR nn(PC)?)
  1594.  
  1595. JSR xx;RTS    JMP xx            save a RTS
  1596. BSR xx;RTS    BRA xx            same...
  1597.                     (assuming routine doesn't rely
  1598.                     on anything in the stack)
  1599.  
  1600. LSL/ASL #1/2,xx    ADD xx,xx [ADD xx,xx]    lsl #2,d0 -> 2 times add d0,d0
  1601.  
  1602. MULU #yy,xx where yy is a power of 2, 2..256
  1603.         LSL/ASL #1-8,xx        mulu #2,d0 -> asl #1,d0 -> add d0,d0
  1604.                     BEWARE: STATUS FLAGS ARE "WRONG"
  1605.  
  1606. DIVU #yy,xx where yy is a power of 2, 2..256
  1607.         LSR/ASR #.. SWAP    divu #16,d0 -> lsr #4,d0
  1608.                     BEWARE: STATUS FLAGS ARE "WRONG",
  1609.                     AND HIGHWORD IS NOT THE REMAINDER.
  1610.  
  1611. ADDRESS-RELATED OPTIMIZATIONS
  1612. RULE: use short adressing/quick adds!
  1613. ----------------------------------------------------------------------------
  1614.  
  1615. MOVEA.L #nn    MOVEA.W #nn        Movea is "sign-extending" thus
  1616.                     possible if 0<=nn<=$7fff
  1617.  
  1618. ADDA.X #nn    LEA nn(            adda.l #800,a0 -> lea 800(a0),a0
  1619.                     possible if -$8000<=nn<=$7fff
  1620.  
  1621. LEA nn(        ADDQ.W #nn        lea 6(a0),a0 -> addq.w #6,a0
  1622.                     possible if 1<=nn<=8
  1623.  
  1624. $0000nnnn.l    $nnnn.w            move.l    4,a6 -> move.l 4.w,a6
  1625.                     possible if 0<=nnnn<=$7fff
  1626.                     (nnnn is SIGN EXTENDED to LONG!)
  1627.  
  1628. MOVE.L #xx,Ay    LEA xx,Ay        try xx(PC) with the LEA
  1629.  
  1630. MOVE.L Ax,Ay;
  1631. ADD #nnnn,Ay    LEA nnnn(Ax),Ay        copy&add in one
  1632.  
  1633. OFFSET-RELATED OPTIMIZATIONS
  1634. RULE: use PC-relative addressing or basereg addressing!
  1635.       put your code&data in ONE segment if possible!
  1636. ----------------------------------------------------------------------------
  1637. MOVE.X nnnn    MOVE.X nnnn(pc)        lea copper,a0 -> lea copper(pc),a0..
  1638. LEA nnnn    LEA nnnn(pc)        ...possible if nnnn is close to PC
  1639.  
  1640. (Ax,Dx.l)    (Ax,Dx.w)        possible if 0<=Dx<=$7fff
  1641.  
  1642. If PC-relative doesn't work, use Ax as a pointer to your data block.
  1643. Use indirect addressing to get to your data: move.l Data1-Base(Ax),Dx etc.
  1644.  
  1645. TRICKY OPTIMIZATIONS
  1646. ----------------------------------------------------------------------------
  1647. BSET #xx,yy    ORI.W    #2^xx,yy    0<=xx<=15
  1648. BCLR #xx,yy    ANDI.W    #~(2^xx),yy    "
  1649. BCHG #xx,yy    EORI.W    #2^xx,yy    "
  1650. BTST #xx,yy    ANDI.W    #2^xx,yy    "
  1651.                     Best improvement if yy=a data reg.
  1652.                     BEWARE: STATUS FLAGS ARE "WRONG".
  1653.  
  1654. SILLY OPTIMIZATIONS (FOR OPTIMIZING COMPILER OUTPUTS ETC)
  1655. RULE: make the routines in assembly yourself!
  1656. ----------------------------------------------------------------------------
  1657. MOVEM (one reg.) MOVE            movem.l    d0,-(sp) -> move d0,-(sp)
  1658.  
  1659. MOVE xx,-(sp)    PEA xx            possible if xx=(Ax) or constant.
  1660.  
  1661. 0(Ax)        (Ax)            -
  1662.  
  1663. MULU/MULS #0    CLR.L            moveq #0,Dx with data-registers.
  1664.  
  1665. MULU #1,xx    SWAP CLR SWAP        high word is cleared with mulu #1
  1666. MULS #1,xx    SWAP CLR SWAP EXT.L    see MULU, and sign exteded.
  1667.                     BEWARE: STATUS FLAGS ARE "WRONG"
  1668.  
  1669. LOOP OPTIMIZATION.
  1670. ----------------------------------------------------------------------------
  1671. Example: imagine you want to eor 4096 bytes beginning at (a0).
  1672. Solution one:
  1673.  
  1674.     move    #4096-1,d7
  1675. ..1    eori.b    d0,(a0)+
  1676.     dbra    d7,.1
  1677.  
  1678. Consider the loop from above. 4096 times a eor.b and a dbra takes time.
  1679. What do you think about this:
  1680.  
  1681.     move    #4096/4-1,d7
  1682. ..1    eor.l    d0,(a0)+
  1683.     dbra    d7,.1
  1684.  
  1685. Eors 4096 bytes too! But only needs 1024 eor.l/dbras.
  1686. Yeah, I hear you smart guys cry: what about 1024 eor.l without any loop?!
  1687. Right, that IS the fastest solution, but is VERY memory consuming (2 Kb).
  1688. Instead, join a loop and a few eor.l:
  1689.  
  1690.     move    #4096/4/4-1,d7
  1691. ..1    eor.l    d0,(a0)+
  1692.     eor.l    d0,(a0)+
  1693.     eor.l    d0,(a0)+
  1694.     eor.l    d0,(a0)+
  1695.     dbra    d7,.1
  1696.  
  1697. This is faster than the loop before. I think about 8 or 16 eor.l's is just
  1698. fine, depending on the size of the mem to be handled (and the wanted
  1699. speed!). Also, mind the cache on 68020+ processors, the loop code must be
  1700. small enough to fit in it for highest speeds.
  1701. Try to do as much as possible within one loop (but considering the text
  1702. above) instead of a few loops after each other.
  1703.  
  1704. MEMORY CLEARING/FILLING.
  1705. ----------------------------------------------------------------------------
  1706. A common problem is how to clear or fill some mem in a short time.
  1707. If it is CHIP-MEMORY, use the blitter (only D-channel, see below). In this
  1708. case you can still do other things with yer 680x0 while blittie-boy is busy
  1709. erasing. If it is FAST-MEMORY, you can use the method from above, with
  1710. clr.l instead of eor.l, but there is a much faster way:
  1711.  
  1712.     move.l    sp,TempSp
  1713.     lea    MemEnd,sp
  1714.     moveq    #0,d0
  1715.     ...for all 7 data regs...
  1716.     moveq    #0,d7
  1717.     move.l    d0,a0
  1718.     ...for 6 address regs...
  1719.     move.l    d0,a6
  1720.  
  1721. After this, ONE instruction can clear 60 bytes of memory (15*4):
  1722.  
  1723.     movem.l    d0-d7/a0-a6,-(sp)    ;wham!
  1724.  
  1725. Now, repeat this instruction as often as required to erase the memory.
  1726. (memsize/60 times). You may need an additional movem.l to erase the last
  1727. few bytes. Get sp(=a7) back at the end with (guess..):
  1728.  
  1729.     move.l    TempSp,sp
  1730.  
  1731. If you are low on mem, put a few movem.l in a loop. But, now you need a
  1732. loop-counter register, so you'll only clear 56 bytes in one movem.l.
  1733. In the case of CHIP memory, you can use both the blitter and the processor
  1734. simultaneously to clear much CHIP mem in a VERY short time...
  1735. It takes some experimentation to find the best sizes to clear with the
  1736. blitter and with the processor.
  1737.  
  1738. BUT, ALWAYS USE A WAITBLIT AFTER CLEARING SIMULTANEOUSLY, even if you know
  1739. that the blitter is finished before your processor is (mind 680x0's)
  1740.  
  1741. BLITTER SPEEDS. (from the Hardware Reference Manual)
  1742. ----------------------------------------------------------------------------
  1743. Some general notes about blitter speeds. These numbers are for an OCS/ECS
  1744. blitter only, in 16-bit chip ram (who knows the AGA blitter speed???)
  1745.  
  1746.               n * H * W
  1747.     time taken = -----------
  1748.             7.09         (7.15 for NTSC)
  1749.  
  1750. time is in microseconds. H=blitheight,W=blitwidth(#words),n=cycles
  1751.  
  1752. n=4+....depends on # DMA-channels used
  1753.  
  1754.     A: +0 (this one is free!)
  1755.     B: +2
  1756.     C or D: +0        In line-mode, every pixel takes 8 cycles.
  1757.     C and D: +2
  1758.  
  1759. So, use A,D,A&D for the fastest operation.
  1760. Use A&C for 2-source operations (e.g. collision check or so).
  1761.  
  1762.  
  1763. NOTES (FURTHER NOTES MAY BE ADDED IN FUTURE...)
  1764. ----------------------------------------------------------------------------
  1765. - 68020+ processors are particularly fast at using longwords. Byte access
  1766.   is some sort of brake on the memory access. Use at least words.
  1767.  
  1768. - 68010 has a loop-cache, it caches 3 word loops like
  1769.     loop    move.l    (a0)+,(a1)+
  1770.         dbra    d7,loop
  1771.  
  1772. - When optimizing BIG programs (for instance, compiler outputs...) first
  1773.   try to find the time-critical parts (inner loops, often called procs etc.)
  1774.   In most cases 10% of the code is responsible for 90% of the execution time.
  1775.   I see people using OldOpenLibrary() because it needs one less register
  1776.   set up.. I mean, what's the point? Are people really going to notice if
  1777.   your demo takes two clock cycles less before starting? :-)
  1778.  
  1779. - Often it is better not to set BLTPRI in DMACON (#10 in $dff09a) as this
  1780.   can keep your processor from calculating things while the blitter is busy.
  1781.  
  1782. - Use as much registers as possible! I.e. store values in registers rather
  1783.   than in memory, this gives one hell of a performance boost.
  1784.   (NOTE: just this is the power of RISC machines. Very much register access
  1785.   instead of memory access. Fill these 16 registers!)
  1786.  
  1787. - Related to the last one: unlike many compilers, DONT put your parameters
  1788.   on stack before calling a sub! Instead, put them in well defined registers!
  1789.  
  1790. - In case you have enough memory, try to remove as many MULU/S and DIVU/S
  1791.   as possible by pre-calculating a multiplication or division table, and
  1792.   reading values from it, rather than each time MULU #10 or so.
  1793.    * Beware on A1200 though, read Chris's section on 68020 optimization.
  1794.  
  1795.  
  1796. 38. How do I make a RESET
  1797. =========================
  1798.  
  1799. Here is the official routine supported by Commodore:
  1800.                              ^^^^^^^^^^^^^^^^^^^^^^
  1801.  
  1802.  
  1803.   INCLUDE "exec/types.i"
  1804.   INCLUDE "exec/libraries.i"
  1805.  
  1806.   csect text
  1807.   xdef  _ColdReboot
  1808.   xref  _LVOSupervisor
  1809.  
  1810. EXECBASE        equ 4
  1811. ROMEND          equ $01000000
  1812. SIZE_OFFSET     equ -$14
  1813.  
  1814. KICK_V36        equ 36
  1815. V36_ColdReboot  equ -726
  1816.  
  1817.  
  1818. _ColdReboot:
  1819.       move.l EXECBASE,a6
  1820.       cmp.w  #KICK_V36,LIB_VERSION(a6)   ;which Version of Exec ?
  1821.       blt.s  .old_kick                   ;old one -> goto old_kick
  1822.  
  1823.       jmp    V36_ColdReboot(a6)          ;else use Exec-Function
  1824.  
  1825. .old_kick:
  1826.       lea    .Reset_Code(pc),a5
  1827.       jsr    _LVOSupervisor(a6)          ;get Supervisor-status
  1828.       ;never reaching this point
  1829.  
  1830.     cnop 0,4                             ;very important
  1831. .Reset_Code:
  1832.       lea    ROMEND,a0                   ;Calc Entrypoint
  1833.       sub.l  SIZE_OFFSET(a0),a0
  1834.       move.l 4(a0),a0
  1835.       subq.l #2,a0
  1836.       reset                              ;Reset peripherie
  1837.       jmp    (a0)                        ;done
  1838.                      ; <reset> and <jmp> in the same LONGWORD !!!!
  1839. END
  1840.  
  1841.  
  1842. 39. System-Privates:
  1843. ====================
  1844.  
  1845.   If anywhere in the manuals, includes or autodocs it says that
  1846.   this or that is  PRIVATE or RESERVED or INTERNAL (or something
  1847.   similar) then
  1848.  
  1849.     * don't read this entry
  1850.     * never ever WRITE something to it
  1851.     * if it's a function, then DON'T use it
  1852.     * don't check it for anything
  1853.  
  1854.   Private system points can be changed without reason, or without
  1855.   writing it into any documentation !
  1856.  
  1857.   (Thanks Arno!)
  1858.  
  1859.  And to add to that, if a system structure member has a routine that
  1860. allows you to alter it (for example, SetAPen() alters the Pen
  1861. value in the RastPort. It is currently possible to alter the pen
  1862. by poking the structure) then USE IT! Do not Poke system structures
  1863. unless there is no other way to alter the value.
  1864.  
  1865. 40. Good Books
  1866. ==============
  1867.  
  1868. I've been asked to suggest some good books:
  1869.  
  1870. Hardware Reference Manual
  1871. -------------------------
  1872. Essential for demo and game coders.
  1873.  
  1874. Rom Kernal Manual: Libraries
  1875. ----------------------------
  1876. Essential for *ALL* Amiga Programmers
  1877.  
  1878. Rom Kernal Manual: Devices
  1879. --------------------------
  1880. Essential if you plan to do any work with Device IO (input.device,
  1881. timer.device, trackdisk.device, etc...)
  1882.  
  1883. Rom Kernal Manual: Includes & Autodocs
  1884. --------------------------------------
  1885. These are available on disk instead, which is a lot cheaper!
  1886. Essential reference work,
  1887.  
  1888. All these books are available to developers on the CATS CD 2 as
  1889. AmigaGuide files.. $50 from CATS US.
  1890.  
  1891. Amiga User Interface & Style Guide
  1892. ----------------------------------
  1893. Probably the most boring book I've ever read :-) Useful if you
  1894. intend to write applications, but even then some of the rules have
  1895. changed for V39 since this book was printed.
  1896.  
  1897. AmigaDOS manual 3rd Edition (Bantam)
  1898. ------------------------------------
  1899. Truly awful book, unfortunately the ONLY official dos.library
  1900. reference. Why it can't be integrated into the RKM's I don't know...
  1901. If you need to program dos.library and want info on AmigaDos file
  1902. and hunk formats, this is the book.
  1903.  
  1904. Mapping the Amiga (Compute)
  1905. ---------------------------
  1906. One of my favourite books. This is an easy-to-read reference
  1907. to all system (1.3) functions and structures. Much easier to
  1908. use than the Includes & Autodocs. I wish there was a V39 update
  1909. to this!
  1910.  
  1911. Amiga System Programmers Guide (Abacus)
  1912. ---------------------------------------
  1913. Quite handy, it covers a lot of the Hardware Reference manual,
  1914. Rom Kernal Manuals and more in one book, but I'd suggest you
  1915. buy the official books instead.
  1916.  
  1917. Advanced Amiga System Programmers Guide (Abacus)
  1918. ------------------------------------------------
  1919. Slightly more interesting than the first one, covers mainly
  1920. OS level programming, but again nothing really new.
  1921.  
  1922. Amiga Disk Drives Inside and Out (Abacus)
  1923. -----------------------------------------
  1924. AVOID THIS BOOK! It has some of the worst code and coding practices
  1925. I have ever seen in it. Half of the code will only work under
  1926. Kickstart 1.2, the other half doesn't work at all!!!!
  1927.  
  1928. 680x0 Programming by Example (Howard Sams & Company)
  1929. ----------------------------------------------------
  1930. Excellent book on 68000 programming. Covers 68000/020/030
  1931. instructions, optimization. Aimed at the advanced 68000 user,
  1932. some really neat stuff in this book. The only 68000 book I've
  1933. bought, except the Motorola manual.
  1934.  
  1935. The Discworld Series (Terry Pratchett)
  1936. --------------------------------------
  1937. Nothing to do with Amigas, but excellent books. If you need
  1938. a break from programming, read one of these!
  1939.  
  1940.  
  1941.  
  1942.  
  1943.  
  1944. Copper Startup Code
  1945. ===================
  1946.  
  1947. I've seperated this out now, cut out this file and keep it safe
  1948. (you may need a grown up to help you with this :-)
  1949.  
  1950. 8<-------8<-------8<-------8<-------8<-------8<-------8<-------8<-------
  1951.  
  1952. *
  1953. * Startup.asm  - A working tested version of startup from Howtocode5.txt
  1954. *
  1955. * This code sets up one of two copperlists (one for PAL and one for NTSC)
  1956. * machines. It shows something to celebrate 3(?) years since the Berlin
  1957. * wall came down :-) Press left mouse button to return to normality.
  1958. * Tested on Amiga 3000 (ECS/V39 Kickstart) and Amiga 1200 (AGA/V39)
  1959. * Read Howtocode5.txt for information on this source!
  1960. *
  1961. * Note: You will have to do something about sprites. Each sprite
  1962. * pointer should point at a valid sprite, or for AGA *FOUR* long
  1963. * words on a 64-bit boundary,
  1964. * ie:
  1965. *        CNOP 0,8
  1966. * blanksprite:  dc.l 0,0,0,0
  1967. *
  1968. * Also, for AGA, sprites need to be fixed (see section on Sprites)
  1969. *
  1970. * $VER: startup.asm V5.0gti (18.3.92)
  1971. * Valid on day of purchase only. No re-admission. No rain-checks.
  1972. *
  1973.  
  1974.     opt    l-,o+                 ; auto link, optimise on
  1975.  
  1976. ;     opt    o3-            ; add this for Devpac Assembler
  1977.  
  1978.     section    mycode,code        ; need not be in chipram
  1979.  
  1980.     incdir    "include:"
  1981.     include    "exec/types.i"
  1982.     include    "exec/funcdef.i"    ; keep code simple and
  1983.     include    "exec/exec_lib.i"    ; easy to read - use
  1984.     include    "graphics/gfxbase.i"    ; the includes!
  1985.     include    "graphics/graphics_lib.i"
  1986.  
  1987.     include "misc/easystart.i"    ; Allows startup from
  1988.                     ; icon
  1989.  
  1990.  
  1991. StartCopper:
  1992.  
  1993.  
  1994.     move.l    4.w,a6
  1995.     sub.l   a1,a1          ; Zero - Find current task
  1996.     jsr    _LVOFindTask(a6)
  1997.  
  1998.     move.l    d0,a1
  1999.     moveq    #127,d0      ; task priority to very high...
  2000.     jsr    _LVOSetTaskPri(a6)
  2001.  
  2002.     move.l    4.w,a6        ; get ExecBase
  2003.     lea    gfxname,a1    ; graphics name
  2004.     moveq    #0,d0        ; any version
  2005.     jsr    _LVOOpenLibrary(a6)
  2006.     tst.l    d0
  2007.     beq    End        ; failed to open? Then quit
  2008.     move.l    d0,gfxbase
  2009.     move.l    d0,a6
  2010.     move.l    gb_ActiView(a6),wbview
  2011.                 ; store current view address
  2012.                                 ; gb_ActiView = 34
  2013.  
  2014. .loop    sub.l    a0,a0        ; clear a0
  2015.     jsr     _LVOLoadView(a6)     ; Flush View to nothing
  2016.     jsr    _LVOWaitTOF(a6)     ; Wait once
  2017.     jsr    _LVOWaitTOF(a6)     ; Wait again.
  2018.  
  2019. ;    now check nothing has run in the meantime!
  2020. ;
  2021. ;  Please note, I haven't actually checked this bit!! :-)
  2022. ;  Can someone prove if it does or does not work????
  2023. ;
  2024.         cmp.l    #0,gb_ActiView(a6)      ; Any other view appeared?
  2025.     bne.s    .loop            ; If so wipe it.
  2026.  
  2027.     cmp.b    #50,VBlankFrequency(a6) ; Am I *running* PAL?
  2028.     bne.s    .ntsc
  2029.  
  2030.     move.l    #mycopper,$dff080     ; bang it straight in.
  2031.     bra.s    .lp
  2032.  
  2033. .ntsc    move.l    #mycopperntsc,$dff080
  2034.  
  2035.  
  2036. .lp    btst    #6,$bfe001              ; ok.. I'll do an input
  2037.     bne.s    .lp                     ; handler next time.
  2038.  
  2039.  
  2040. CloseDown:
  2041.     move.l    wbview,a1
  2042.     move.l    gfxbase,a6
  2043.     jsr    _LVOLoadView(a6) ; Fix view
  2044.  
  2045.     move.l    gb_copinit(a6),$dff080     ; Kick it into life
  2046.                                          ; copinit = 38
  2047.     move.l    a6,a1
  2048.     move.l    4.w,a6
  2049.     jsr    _LVOCloseLibrary(a6) ; EVERYONE FORGETS THIS!!!!
  2050.  
  2051. End:    rts                              ; back to workbench/clc
  2052.  
  2053.     section mydata,data_c             ;  keep data & code seperate!
  2054.  
  2055. mycopper    dc.w    $100,$0200    ; otherwise no display!
  2056.             dc.w    $180,$00
  2057.             dc.w    $8107,$fffe        ; wait for $8107,$fffe
  2058.         dc.w    $180,$f00          ; background red
  2059.         dc.w    $d607,$fffe    ; wait for $d607,$fffe
  2060.         dc.w    $180,$ff0          ; background yellow
  2061.             dc.w    $ffff,$fffe
  2062.             dc.w    $ffff,$fffe
  2063.  
  2064. mycopperntsc
  2065.         dc.w    $100,$0200    ; otherwise no display!
  2066.             dc.w    $180,$00
  2067.             dc.w    $6e07,$fffe        ; wait for $6e07,$fffe
  2068.             dc.w    $180,$f00          ; background red
  2069.         dc.w    $b007,$fffe    ; wait for $b007,$fffe
  2070.         dc.w    $180,$ff0          ; background yellow
  2071.             dc.w    $ffff,$fffe
  2072.         dc.w    $ffff,$fffe
  2073.  
  2074. wbview      dc.l    0
  2075. gfxbase     dc.l    0
  2076. gfxname     dc.b    "graphics.library",0
  2077.  
  2078.  
  2079. Thanks to everyone who has replied. Any more questions, queries,
  2080. or "CJ, you got it wrong again!" type mail to the email
  2081. address below....
  2082.  
  2083. What I want:
  2084. ===========
  2085. If anyone wants to spend some time writing something on these
  2086. (especially from a demo coders perspective) I'd be very grateful.
  2087. I would write some of them myself if I had more time...
  2088.  
  2089.  
  2090. o    68881/2 Programming
  2091.  
  2092. o    How to Read C code (for Assember programmers reading OS manuals)
  2093.  
  2094. o    Introduction to programming vector graphics
  2095.  
  2096. o    How to set up an input handler
  2097.  
  2098. o    Reading the new Motorola syntax code
  2099.  
  2100.  
  2101. And anything else you want to write about. Please feel free to write
  2102. additions/replacements for anything already here...
  2103.  
  2104.  
  2105.  
  2106. And of course, if anyone spots a *really* bad bit of code or
  2107. programming practice, let me know and I'll warn people about
  2108. it here... (Don't send my old code though :-)
  2109.  
  2110. And a final comment: For those of you who wrote about
  2111. Amazing Tunes II (a demo I wrote 2 years back) wanting to
  2112. know how to get it to run on a 1Mb chipram machine... Sorry.
  2113. It doesn't. It probaby breaks *every* rule in this docfile.
  2114. I speak from experience. I used to be that evil programmer :-)
  2115. Disassemble the bootblock to see some nightmare code... You can
  2116. probably patch it if you're clever. It was meant to support
  2117. 1Mb chipram but it never worked..  If you have 1.5Mb of ram
  2118. (be it chip, fast or a mixture) it should work though...
  2119.  
  2120. I had to totally rewrite it recently for The Demo Collection CDROM
  2121.  
  2122. (which is totally amazing, costs £19.95 and contains 600Mb
  2123. of demos, animations, 1000 Modules and much more - It's available
  2124. from Almathera on (UK) 081 683 6418)
  2125.  
  2126. It now plays 1000 modules (instead of 20), is much more system
  2127. friendly and works on a (less than) 1Mb chipram CDTV.
  2128.  
  2129. And you never know, if enough people ask I may do an AGA version,
  2130. possibly on an Intuition screen... That would be nice!
  2131.  
  2132. --------------------------------------------------------------------
  2133.  
  2134. This text is Copyright (C) 1993 Share and Enjoy, but may be freely
  2135. distributed in any electronic form. The copyright of contributions
  2136. quoted from other authors remains with the original author. If you would
  2137. like to contribute to this file, email me at the address below...
  2138.  
  2139. The startup code in this article is freeware and may be used by
  2140. anyone for any purpose.
  2141.  
  2142. All trademarks and registered names (Workbench, Kickstart, etc)
  2143. acknowledged.
  2144.  
  2145. All opinions expressed in this article are my own, and in no way
  2146. reflect those of anyone else. Please note that many of the
  2147. programming practices described in this text are ONLY applicable
  2148. for demo coding, and should not be used for Games and other
  2149. programming.
  2150.  
  2151. I didn't write this for fun, I wrote it for you to use! Hopefully
  2152. this will grow into a big file that demo coders can use.
  2153.  
  2154. If you want to make a contribution please email it to me:
  2155. I prefer plain ASCII set to no more than 75 column width, and
  2156. no tabs if possible (although I can fix text sent to me..)
  2157.  
  2158. If you strongly disagree with anything I write, or you want to send me
  2159. some source or demos to test on Amiga 1200/4000 etc, or you have
  2160. questions about Amiga programming, or suggestions for future articles,
  2161. or just want to chat about the best way to optimise automatic copperlist
  2162. generation code (*), then contact me via email at:
  2163.  
  2164. Please send mail to comradej@althera.demon.co.uk.
  2165.  
  2166. I CAN NOW REPLY TO MAIL!!! At last, thanks to AmigaElm and some ARexx
  2167. I can reply to mail! If you sent me a message and haven't got a reply,
  2168. it's because I lost the message, please mail me again! Sorry about
  2169. the delays before!
  2170.  
  2171. I seem to have lost usenet news now, so I haven't read anything from
  2172. alt.sys.amiga.demos since early January.
  2173.  
  2174. * - This is a NIGHTMARE. I really feel sorry for the guys who wrote
  2175. MrgCop(). I will never swear at MrgCop() or RethinkDisplay() again :-)
  2176.  
  2177.